package com.mxchip.livestarmobile.mobile.ui.recent

import android.content.DialogInterface
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.mxchip.livestarmobile.mobile.CallActivityA
import com.mxchip.livestarmobile.R
import com.mxchip.livestarmobile.mobile.db.entity.CallLog
import com.mxchip.livestarmobile.mobile.db.entity.CallLogWithContact
import kotlinx.android.synthetic.main.fragment_rencent.*

class RecentFragment : Fragment() {
    private val TAG = "RecentFragment"
    private val recentViewModel by activityViewModels<RecentViewModel>()

    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?, savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_rencent, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.adapter = RecentRecyclerAdapter(onClickListener = this::onItemClick)


        recentViewModel.callLogs.observe(viewLifecycleOwner, Observer { list ->
            Log.d("RecentFragment", "" + list)
        })
        recentViewModel.callLogWithContact.observe(viewLifecycleOwner, Observer { list ->
            Log.d("RecentFragment", "callLogWithContact:$list")

            val adapter = recyclerView.adapter

            if (adapter is RecentRecyclerAdapter) {
                adapter.data = list
                adapter.notifyDataSetChanged()
            }
        })
    }


    private fun openContactDetailActivity(callLog: CallLog) {

        val number = callLog.number ?: return

        recentViewModel.insertCallLog(number)
        val call = recentViewModel.makeCall(number)
        context?.let { CallActivityA.open(it, call) }
    }


    private fun onItemClick(view: View, bean: CallLogWithContact) {
        val callDialog = RecentCallDialog(requireContext())
        callDialog.setBean(bean.callLog)
        callDialog.setOnClickListener { dialog: DialogInterface, which: Int ->
            Log.e(TAG, "which: $which")
            when (which) {
                0 -> {
                    openContactDetailActivity(bean.callLog)
                }
                1 -> {
                    val number = bean.callLog.number
//                    addContact(number)
                }
                2 -> {
                    val id = bean.callLog.uid
                    recentViewModel.deleteCallLogById(id)
                }
            }
            dialog.dismiss()
        }
        callDialog.setOnDismissListener { dialog: DialogInterface -> (dialog as RecentCallDialog).setOnClickListener(null) }
        callDialog.show()
    }


}